import cloud from '@lafjs/cloud'
import Pay from 'wechatpay-node-v3-laf';
let appid

export default async function (ctx: FunctionContext) {
  let { payType, out_trade_no } = ctx.body
  if (payType == "MP-WEIXIN") {
    appid = "wx9c3827b9e4e2ea96"
  } else {
    appid = "wx267097d752f5c3fd"
  }

  const pay = new Pay({
    appid, // 认证服务号 appid
    mchid: '1593154581',  //  绑定该认证服务号的微信支付商户号
    publicKey: savePublicKey(), // V3 公钥 apiclient_cert.pem
    privateKey: getPrivateKey(), // V3 秘钥 apiclient_key.pem
  });

  const result = await pay.query({ out_trade_no });
  return cloud.utils.requestReturn({ data: result });
}


function getPrivateKey() {
  const key = `-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCf2rhKEaSNKHVN
i4VWD7zpqNcM3O3NPdLyxAR4ie96eCzLzvxzpUY2Oy03yA6qoWfV6KnvaN2uqNWp
ng041W11MUZIR07bKyXauapfKz1X+5aJa5qrlsiNP3/rzSoqxCTtySKuoeTG2/u7
QpipTZ7L7Rzy/6pI2gbr53f7gxCErfyI2FsbzhSXnqOeUDMvq24EwtJCnYlSi3p7
LRv44qT6l0MNfZUY7pZX/0SJ7WdsUfZQOtxNpYvrRXYgvDg67Y67tcPv0e1kvs0F
y5rxk82sQEA3+avpHjfsn+0+TrzrS5hKKiOlERDfCZMV79MTeJA3yyEdIs24g9sb
DIm2L11zAgMBAAECggEAZ76GXiPHHImGX7ZN6cW7ABZAwyta9hnKnx2SHTy//Eak
I/2In/FCeQ280ff3dMvd3Njr+OGhBaqzSCq4QAtbx3QcPepxhhIhtitLJAqm/EPe
tf7s2bG5JNpyRfHYDPtRUDV6WcXd+SVp4WVeOHqUtZqEcDx3ImLFnTKTDttHzCMs
J53W0nLQI3mZ0ADeua1DpEF3a2IrGTKRqIBxTHGlm5qniYCvpHImv+dTrcykq+Kx
0SSYLKYe3SstDsQUN+5jG0T1TIxm1Gwb9UW7AuK29YbTuE2T7Qa/T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